﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLyDaiLy.Model;
using QuanLyDaiLy.Context;
using QuanLyDaiLy.Platform;

namespace QuanLyDaiLy.View
{
    /// <summary>
    /// Interaction logic for WQuan.xaml
    /// </summary>
    public partial class WQuan : Window
    {
        private CQuan quan;
        private CQuanContext quanContext;
       
        #region Function

        public WQuan()
        {
            InitializeComponent();
            InitializeFeature();
        }

        private void InitializeFeature()
        {
            quanContext = new CQuanContext(CConstant.CONNECTION);
            try
            {
                TaoBangDuLieu();
                TrangThaiMacDinh();
                CapNhatDuLieuBang();
            }
            catch { }
        }

        private void TrangThaiMacDinh()
        {
            txtMaQuan.IsEnabled = false;
            txtTenQuan.IsEnabled = false;
            btLuu.IsEnabled = false;
            btXoa.IsEnabled = false;
            txtMaQuan.Clear();
            txtTenQuan.Clear();
        }

        private void TrangThaiThem()
        {
            txtTenQuan.IsEnabled = true;
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = false;
            txtTenQuan.Clear();  
            txtTenQuan.Focus();
            txtMaQuan.Text = quanContext.LayMaSoMoi().ToString();
        }

        private void TrangThaiChon()
        {
            btLuu.IsEnabled = true;
            btXoa.IsEnabled = true;
            txtTenQuan.IsEnabled = true;
        }

        private bool KiemTraNhap()
        {
            if (txtTenQuan.Text == "")
            {
                MessageBox.Show("Vui Lòng Nhập Tên Quận");
                txtTenQuan.Focus();
                return false;
            }

            return true;
        }

        private void LuuQuan()
        {
            try
            {
                if (KiemTraNhap())
                {
                    quan = new CQuan(int.Parse(txtMaQuan.Text) ,txtTenQuan.Text);
                    if (Convert.ToInt32(txtMaQuan.Text) < quanContext.LayMaSoMoi())
                    {
                        var result = MessageBox.Show("Bạn Có Chắc Chắn Sửa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (result == MessageBoxResult.Yes)
                        {
                            quanContext.CapNhat(quan);
                            MessageBox.Show("Sửa Thành Công");
                        }
                        TrangThaiChon();
                    }
                    else
                    {
                        quanContext.Them(quan);
                        MessageBox.Show("Lưu Thành Công");
                        TrangThaiThem();
                    }

                    CapNhatDuLieuBang();
                }
            }
            catch { }
                
        }

        private void XoaQuan()
        {
            try
            {
                var result = MessageBox.Show("Bạn Có Chắc Chắn Xóa Không?", "Thông Báo", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                    quanContext.Xoa(Convert.ToInt32(txtMaQuan.Text));
                    CapNhatDuLieuBang();
                    TrangThaiMacDinh();
                    MessageBox.Show("Xóa Thành Công");                 
                }
            }
            catch { MessageBox.Show("Chưa Thể Xóa"); }
        }

        private void ChonQuan()
        {
            try
            {
                TrangThaiChon();
                if (lvQuan.SelectedItem is CQuan)
                {
                    txtMaQuan.Text = (lvQuan.SelectedItem as CQuan).MaQuan.ToString();
                    txtTenQuan.Text = (lvQuan.SelectedItem as CQuan).TenQuan;
                }
            }
            catch { }
        }

        private void TaoBangDuLieu()
        {
            try
            {
                GridView grdView = new GridView();
                grdView.AllowsColumnReorder = true;
                grdView.ColumnHeaderToolTip = "QUAN";

                GridViewColumn maquan = new GridViewColumn();
                maquan.DisplayMemberBinding = new Binding("MaQuan");
                maquan.Header = "Mã Quận";
                maquan.Width = 120;
                grdView.Columns.Add(maquan);

                GridViewColumn tenquan = new GridViewColumn();
                tenquan.DisplayMemberBinding = new Binding("TenQuan");
                tenquan.Header = "Tên Quận";
                tenquan.Width = 120;
                grdView.Columns.Add(tenquan);

                lvQuan.View = grdView;
            }
            catch { }
        }

        private void CapNhatDuLieuBang()
        {
            lvQuan.ItemsSource = quanContext.LayDanhSachTatCa();
        }

        #endregion

      
       

        #region EVEN

        private void btThemQuan_Click(object sender, RoutedEventArgs e)
        {
            TrangThaiThem();
        }

        private void btLuu_Click(object sender, RoutedEventArgs e)
        {
            LuuQuan();
        }

        private void btXoa_Click(object sender, RoutedEventArgs e)
        {
            XoaQuan();
        }

        private void lvQuan_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ChonQuan();
        }

        #endregion

        private void txtTenQuan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LuuQuan();
            }
        }

       

      

       
    }
}
